import unittest
import subprocess
import tempfile
import os
import yaml

class TestRegression(unittest.TestCase):
    """回归测试"""
    
    def setUp(self):
        self.splitter_cmd = ['python3', '-m', 'tools.main']
    
    def test_known_good_scenario_bash_slice(self):
        """测试已知的bash slice场景"""
        with tempfile.TemporaryDirectory() as tmpdir:
            output_dir = os.path.join(tmpdir, 'output')
            
            cmd = self.splitter_cmd + [
                'cut',
                '-r', '24.03-LTS',
                '-a', 'x86_64',
                '-o', output_dir,
                'bash_bin'
            ]
            
            result = subprocess.run(cmd, capture_output=True, text=True)
            
            # 验证基本成功执行
            print(f"Bash slice test - Return code: {result.returncode}")
            if result.stderr:
                print(f"Stderr: {result.stderr}")
    
    def test_known_good_scenario_python3_gen(self):
        """测试已知的python3 gen场景"""
        with tempfile.TemporaryDirectory() as tmpdir:
            output_dir = os.path.join(tmpdir, 'sdf')
            os.makedirs(output_dir, exist_ok=True)
            
            cmd = self.splitter_cmd + [
                'gen',
                '-r', '24.03-LTS',
                '-o', output_dir,
                '-p', 'python3'
            ]
            
            result = subprocess.run(cmd, capture_output=True, text=True)
            
            # 验证基本成功执行
            print(f"Python3 gen test - Return code: {result.returncode}")
            if result.stdout:
                print(f"Stdout: {result.stdout}")
    
    def test_repeated_execution_stability(self):
        """测试重复执行稳定性"""
        with tempfile.TemporaryDirectory() as tmpdir:
            output_dir = os.path.join(tmpdir, 'output')
            
            # 第一次执行
            cmd1 = self.splitter_cmd + [
                'cut',
                '-r', '24.03-LTS',
                '-a', 'x86_64',
                '-o', output_dir,
                'bash_bin'
            ]
            
            result1 = subprocess.run(cmd1, capture_output=True, text=True)
            
            # 第二次执行（应该有一致的行为）
            result2 = subprocess.run(cmd1, capture_output=True, text=True)
            
            # 验证两次执行结果一致
            self.assertEqual(result1.returncode, result2.returncode)
    
    def test_help_consistency(self):
        """测试帮助信息一致性"""
        # 测试主帮助
        result_main = subprocess.run(self.splitter_cmd + ['--help'], 
                                   capture_output=True, text=True)
        
        # 测试cut帮助
        result_cut = subprocess.run(self.splitter_cmd + ['cut', '--help'], 
                                  capture_output=True, text=True)
        
        # 测试gen帮助
        result_gen = subprocess.run(self.splitter_cmd + ['gen', '--help'], 
                                  capture_output=True, text=True)
        
        # 验证所有帮助命令都成功
        self.assertEqual(result_main.returncode, 0)
        self.assertEqual(result_cut.returncode, 0)
        self.assertEqual(result_gen.returncode, 0)
        
        # 验证帮助信息包含关键内容
        self.assertIn('cut', result_main.stdout)
        self.assertIn('gen', result_main.stdout)
        self.assertIn('Generate slices', result_cut.stdout)
        self.assertIn('Generate a Slice Definition File', result_gen.stdout)